package com.baidu.servlet;

import com.fd.service.LoginService;
import com.fd.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginController extends HttpServlet {
    private LoginService loginService=new LoginServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    //获取登录请求，调用LoginService接口的logins方法，传入页面获取的账号和密码
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        resp.setContentType("text/html;charset=utf-8");
        boolean status = loginService.logins(account, password);
        HttpSession session = req.getSession();
        session.setAttribute("account",account);
        if (status){
            req.getRequestDispatcher("/commodity").forward(req, resp);
        }else{
            resp.getWriter().println("账号或密码错误");
        }
    }
}
